package com.csproj.basic.service;

import com.csproj.basic.domain.Enterprise;
import com.csproj.basic.repository.EnterpriseRepository;
import com.csproj.basic.service.dto.EnterpriseDTO;
import com.csproj.basic.service.mapper.EnterpriseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing Enterprise.
 */
@Service
@Transactional
public class EnterpriseService {

    private final Logger log = LoggerFactory.getLogger(EnterpriseService.class);

    private final EnterpriseRepository enterpriseRepository;

    private final EnterpriseMapper enterpriseMapper;

    public EnterpriseService(EnterpriseRepository enterpriseRepository, EnterpriseMapper enterpriseMapper) {
        this.enterpriseRepository = enterpriseRepository;
        this.enterpriseMapper = enterpriseMapper;
    }

    /**
     * Save a enterprise.
     *
     * @param enterpriseDTO the entity to save
     * @return the persisted entity
     */
    public EnterpriseDTO save(EnterpriseDTO enterpriseDTO) {
        log.debug("Request to save Enterprise : {}", enterpriseDTO);
        Enterprise enterprise = enterpriseMapper.toEntity(enterpriseDTO);
        enterprise = enterpriseRepository.save(enterprise);
        return enterpriseMapper.toDto(enterprise);
    }

    /**
     * Get all the enterprises.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<EnterpriseDTO> findAll() {
        log.debug("Request to get all Enterprises");
        return enterpriseRepository.findAll().stream()
            .map(enterpriseMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

}
